<script setup>
import { Search, Refresh } from '@element-plus/icons-vue'

import { ref } from 'vue'
import {
  adminreviewinfoGetService,
  adminreviewinfoUpService
} from '@/api/admin/review'
const loading = ref(false)

const landresourceviewinfo = ref([])
const landresourceviewinfoview = ref([])
//获取数据库数据
const getreviewInfoList = async () => {
  loading.value = true
  const res = await adminreviewinfoGetService()
  const reviewInfoList = res.data.data

  // 将 status 是 "待审核" 的项放在列表前面
  const sortedList = reviewInfoList.sort((a, b) => {
    // 将待审核的项排在前面
    if (a.status === '待审核' && b.status !== '待审核') {
      return -1
    }
    // 将非待审核的项排在后面
    if (a.status !== '待审核' && b.status === '待审核') {
      return 1
    }
    // 其他情况保持原顺序不变
    return 0
  })
  // 更新 landresourceviewinfo 和 landresourceviewinfoview
  landresourceviewinfo.value = sortedList
  landresourceviewinfoview.value = sortedList

  loading.value = false
}
getreviewInfoList()

//编辑操作
const onAgreesstReviewinfo = async (row) => {
  await ElMessageBox.confirm('是否通过审核?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
    center: true
  })
  adminreviewinfoUpService({ id: row.id, status: 'agress' })
    .then((response) => {
      if (response.data.status == 200) {
        ElMessage.success(response.data.message)
        getreviewInfoList()
      } else {
        ElMessage.error(response.data.message)
      }
    })
    .catch((error) => {
      ElMessage.error(error)
      console.log(error)
    })
}
//删除操作
const drawer = ref(false)
const formModel = ref({
  id: '',
  comments: ''
})
const form = ref()

const rules = {
  comments: [{ required: true, message: '请输入拒绝的原因', trigger: 'blur' }]
}
import { adminreviewinfoNoService } from '@/api/admin/review'
const onNoreviewinfo = async (row) => {
  // await ElMessageBox.confirm('是否拒绝该申请?', '温馨提示', {
  //   confirmButtonText: '确定',
  //   cancelButtonText: '取消',
  //   type: 'warning',
  //   center: true
  // })
  formModel.value.id = row.id
  drawer.value = true
}
const submitForm = async () => {
  //
  await form.value.validate()
  adminreviewinfoNoService(formModel.value)
    .then((response) => {
      if (response.data.status == 200) {
        ElMessage.success(response.data.message)
        getreviewInfoList()
      } else {
        ElMessage.error(response.data.message)
      }
    })
    .catch((error) => {
      ElMessage.error(error)
    })
    .finally(() => {
      drawer.value = false
    })
}
const beforeClose = () => {
  form.value.resetFields()
  drawer.value = false
}

//搜索
const keyowner = ref()
const keyaddress = ref()
const keySearch = () => {
  landresourceviewinfoview.value = []
  landresourceviewinfoview.value = landresourceviewinfo.value.filter((row) => {
    if (keyowner.value && !keyaddress.value) {
      if (row.owner.includes(keyowner.value)) {
        return true
      } else {
        return false
      }
    } else if (!keyowner.value && keyaddress.value) {
      if (row.address.includes(keyaddress.value)) {
        return true
      } else {
        return false
      }
    } else {
      if (
        row.owner.includes(keyowner.value) &&
        row.address.includes(keyaddress.value)
      ) {
        return true
      } else {
        return false
      }
    }
  })
}
//重置搜索条件
const resetFilter = () => {
  landresourceviewinfoview.value = landresourceviewinfo.value
  keyowner.value = ''
  keyaddress.value = ''
}
</script>

<template>
  <page-container title="用户信息">
    <!-- 头部 -->
    <template #extra>
      <strong>土地所在地</strong>
      <el-input
        placeholder="请输入"
        style="width: 200px"
        v-model="keyaddress"
      /><strong>申请用户</strong>
      <el-input placeholder="请输入" style="width: 150px" v-model="keyowner" />

      <el-button :icon="Search" @click="keySearch">搜索</el-button>
      <el-button :icon="Refresh" @click="resetFilter">重置</el-button>
    </template>
    <el-table
      :data="landresourceviewinfoview"
      style="width: 100%"
      v-loading="loading"
      @selection-change="handleSelectionChange"
      ref="table"
    >
      <el-table-column type="index" label="序号" width="100" />
      <el-table-column prop="landresname" label="土地名称" width="150" />
      <el-table-column prop="address" label="土地所在地" width="200" />
      <el-table-column
        prop="parcelcount"
        label="拥有大棚土地块数"
        width="150"
      />
      <el-table-column prop="owner" label="申请用户" width="180" />
      <el-table-column prop="status" label="状态" width="180" />
      <el-table-column fixed="right" label="操作" width="140">
        <template #default="{ row, $index }">
          <el-button
            type="primary"
            size="small"
            plain
            v-if="row.status === '待审核'"
            @click="onAgreesstReviewinfo(row, $index)"
            >同意</el-button
          >
          <el-button
            type="danger"
            size="small"
            plain
            @click="onNoreviewinfo(row, $index)"
            v-if="row.status === '待审核'"
            >拒绝</el-button
          >
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
    <el-drawer
      v-model="drawer"
      direction="btt"
      :before-close="beforeClose"
      size="50%"
      title="拒绝原因"
    >
      <el-form :model="formModel" :rules="rules" ref="form" label-width="450px">
        <el-form-item label="拒绝原因" prop="comments">
          <el-input
            style="width: 440px"
            :rows="5"
            type="textarea"
            v-model="formModel.comments"
            placeholder="请输入拒绝的原因"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm"
            style="margin-left: 180px"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </el-drawer>
  </page-container>
</template>
